'use strict'

define(['jquery', 'bootstrap', 'select2', 'validate', 'validate.en', 'koala-search', 'modalSearch', 
        'shiftform' , 'datetimepicker', 'datetimepickerCN'], function ($, validate) {
  return {

    init: function () {
      this.initPage();
      this.initBusinessEdit();
      this.initPrint();
      this.initPaymentInfoEdit();
    },

    initPage: function () {

      $(document).on("shown.bs.modal", "#create, #edit , #delete", function (e) {

        $(e.currentTarget).find('form[data-validate="true"]').bt_validate()  //bind validate

        var $form = $(e.currentTarget).find("form")              //bind shiftform
        $form.shiftform({
          beforeSubmit: function (el) {
            $form.trigger("validate.ajax.submit");
            return $.bt_validate.result[$.bt_validate.form_id]
          }
        })
        
        var brandVal = $('select[name="brand"]').val();
        
        if("OTHER" != brandVal){
          
          $('input[name="otherBrand"]').val("");
        }
        
        if($('input[name="otherBrand"]').val() != ""){
          
          $('input[name="otherBrand"]').show();
          $("#otherBrandLabel").show();
        }else{
          
          $('input[name="otherBrand"]').hide();
          $("#otherBrandLabel").hide();
        }
        
        
        
        $('select[name="brand"]').on("click", function(e){
          
          var brandVal = $('select[name="brand"]').val().trim();
          if("OTHER" == brandVal){
          
          $('input[name="otherBrand"]').show();
            $("#otherBrandLabel").show();
          }else{
          
          $('input[name="otherBrand"]').hide();
            $("#otherBrandLabel").hide();
          }
          
         })
         
      });
    },
    
    initBusinessEdit: function() {
      $(document).on("shown.bs.modal", "#business-edit", function (e) {

        $(e.currentTarget).find('form[data-validate="true"]').bt_validate()  //bind validate

        var $form = $(e.currentTarget).find("form")              //bind shiftform
        $form.shiftform({
          beforeSubmit: function (el) {
            $form.trigger("validate.ajax.submit");
            return $.bt_validate.result[$.bt_validate.form_id]
          }
        })
         
      });
    },

    initPaymentInfoEdit: function() {
      $(document).on("shown.bs.modal", "#payment-info-edit", function (e) {

        $(e.currentTarget).find('form[data-validate="true"]').bt_validate()  //bind validate

        var $form = $(e.currentTarget).find("form")              //bind shiftform
        $form.shiftform({
          beforeSubmit: function (el) {
            $form.trigger("validate.ajax.submit");
            return $.bt_validate.result[$.bt_validate.form_id]
          }
        })

      });
    },
    
    initPrint: function() {
      $(document).on('click', 'a.print', function() {
        
        var supplierName = $("p.supplier-name").text();

        var headhtml = "<html lang='zh-CN'><head><meta charset='UTF-8'><title>" + supplierName + "</title></head><body>";
        var foothtml = "</body>";
        
        var newhtml= $("#print-page").html();

        // 获取原来的窗口界面body的html内容，并保存起来
        var oldhtml = document.body.innerHTML;

        // 给窗口界面重新赋值，赋自己拼接起来的html内容
        document.body.innerHTML = headhtml + newhtml + foothtml;
        // 调用window.print方法打印新窗口
        window.print();

        // 将原来窗口body的html值回填展示
        document.body.innerHTML = oldhtml;
        return false;
        
      });
    }
    
  }
})